package com.cvpoc.hibernate.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cvpoc.dto.CertificationDTO;
import com.cvpoc.dto.CollaborateurCertificationDTO;
import com.cvpoc.dto.EditeurDTO;
import com.cvpoc.hibernate.dao.CertificationDao;
import com.cvpoc.model.Certification;
import com.cvpoc.model.CollaborateurCertification;
import com.cvpoc.model.Editeur;

public class CertificationDaoImpl extends HibernateDaoSupport implements
		CertificationDao {

	@Override
	public CertificationDTO findCertificationById(int idCertification) {
		Certification certification = (Certification) getHibernateTemplate().get(
				Certification.class, idCertification);
		
		String[] excludeProperties = {"editeur"};
		CertificationDTO certificationDTO = new CertificationDTO();
		certificationDTO.setEditeur(new EditeurDTO());
		
		BeanUtils.copyProperties(certification, certificationDTO,excludeProperties);
		BeanUtils.copyProperties(certification.getEditeur(), certificationDTO.getEditeur());
		
		return certificationDTO;
	}

	@Override
	public List<CollaborateurCertificationDTO> findListCertificationsByIdCollaborateur(
			int idCollaborateur) {
		List<CollaborateurCertificationDTO> collaborateurCertificationDTOs = new ArrayList<CollaborateurCertificationDTO>();
		String[] excludePropertiesCertif = {"certification"};
		String[] excludePropertiesEdit = {"editeur"};
		CollaborateurCertificationDTO target = null;
		
		DetachedCriteria dc = DetachedCriteria.forClass(CollaborateurCertification.class);
		dc.add(Restrictions.eq("id_collaborateur", idCollaborateur));

		List<CollaborateurCertification> collaborateurCertifications = getHibernateTemplate()
				.findByCriteria(dc);

		for (CollaborateurCertification collaborateurCertification : collaborateurCertifications) {
			target = new CollaborateurCertificationDTO();
			target.setCertification(new CertificationDTO());
			target.getCertification().setEditeur(new EditeurDTO());
			BeanUtils.copyProperties(collaborateurCertification, target, excludePropertiesCertif);
			BeanUtils.copyProperties(collaborateurCertification.getCertification(), target.getCertification(),excludePropertiesEdit);
			BeanUtils.copyProperties(collaborateurCertification.getCertification().getEditeur(), target.getCertification().getEditeur());
			collaborateurCertificationDTOs.add(target);
		}

		return collaborateurCertificationDTOs;
	}

	@Override
	public CollaborateurCertificationDTO findCertificationByIdCollaborateur(
			int idCertification, int idCollaborateur) {
		CollaborateurCertificationDTO collaborateurCertificationDTO = new CollaborateurCertificationDTO();
		collaborateurCertificationDTO.setCertification(new CertificationDTO());
		collaborateurCertificationDTO.getCertification().setEditeur(new EditeurDTO());
		String[] excludePropertiesCertif = {"certification"};
		String[] excludePropertiesEdit = {"editeur"};
		
		DetachedCriteria dc = DetachedCriteria.forClass(CollaborateurCertification.class);
		dc.add(Restrictions.eq("id_collaborateur", idCollaborateur));
		dc.add(Restrictions.eq("certification.id", idCertification));
		
		List<CollaborateurCertification> collaborateurCertifications = getHibernateTemplate()
				.findByCriteria(dc);
		if(collaborateurCertifications.size()>0){
			BeanUtils.copyProperties(collaborateurCertifications.get(0), collaborateurCertificationDTO, excludePropertiesCertif);
			BeanUtils.copyProperties(collaborateurCertifications.get(0).getCertification(), collaborateurCertificationDTO.getCertification(),excludePropertiesEdit);
			BeanUtils.copyProperties(collaborateurCertifications.get(0).getCertification().getEditeur(), collaborateurCertificationDTO.getCertification().getEditeur());
		}
				
		return collaborateurCertificationDTO;
	}

	@Override
	public List<CertificationDTO> findAllCertification() {
		List<CertificationDTO> certificationDTOs = new ArrayList<CertificationDTO>();
		String[] excludeProperties = {"editeur"};
		CertificationDTO target = null;

		List<Certification> certifications = getHibernateTemplate().loadAll(Certification.class);
		
		for (Certification certification : certifications) {
			target = new CertificationDTO();
			target.setEditeur(new EditeurDTO());
			BeanUtils.copyProperties(certification, target,excludeProperties);
			BeanUtils.copyProperties(certification.getEditeur(), target.getEditeur());
			certificationDTOs.add(target);
		}

		return certificationDTOs;
	}

	@Override
	public List<CertificationDTO> findListCertificationByEditeur(int idEditeur) {
		List<CertificationDTO> certificationDTOs = new ArrayList<CertificationDTO>();
		String[] excludeProperties = {"editeur"};
		CertificationDTO target = null;

		DetachedCriteria dc = DetachedCriteria.forClass(Certification.class);
		dc.add(Restrictions.eq("editeur.id", idEditeur));

		List<Certification> certifications = getHibernateTemplate()
				.findByCriteria(dc);

		for (Certification certification2 : certifications) {
			target = new CertificationDTO();
			target.setEditeur(new EditeurDTO());
			BeanUtils.copyProperties(certification2, target, excludeProperties);
			BeanUtils.copyProperties(certification2.getEditeur(), target.getEditeur());
			certificationDTOs.add(target);
		}

		return certificationDTOs;
	}

	@Override
	public void createCollaborateurCertification(
			CollaborateurCertificationDTO collaborateurCertificationDTO) {
		CollaborateurCertification collaborateurCertification = new CollaborateurCertification();
		collaborateurCertification.setCertification(new Certification());
		collaborateurCertification.getCertification().setEditeur(new Editeur());
		String[] excludePropertiesCertif = {"certification"};
		String[] excludePropertiesEdit = {"editeur"};
		
		BeanUtils.copyProperties(collaborateurCertificationDTO, collaborateurCertification, excludePropertiesCertif);
		BeanUtils.copyProperties(collaborateurCertificationDTO.getCertification(), collaborateurCertification.getCertification(),excludePropertiesEdit);
		BeanUtils.copyProperties(collaborateurCertificationDTO.getCertification().getEditeur(), collaborateurCertification.getCertification().getEditeur());
		
		getHibernateTemplate().save(collaborateurCertification);
	}

	@Override
	public int createCertification(CertificationDTO certificationDTO) {
		Certification certification = new Certification();
		certification.setEditeur(new Editeur());
		String[] excludeProperties = {"editeur"};

		BeanUtils.copyProperties(certificationDTO, certification,excludeProperties);
		BeanUtils.copyProperties(certificationDTO.getEditeur(), certification.getEditeur());
		int idCreate = (Integer) getHibernateTemplate().save(certification);

		return idCreate;
	}

	@Override
	public void updateCertification(CertificationDTO certificationDTO) {
		Certification certification = new Certification();
		certification.setEditeur(new Editeur());
		String[] excludeProperties = {"editeur"};

		BeanUtils.copyProperties(certificationDTO, certification,excludeProperties);
		BeanUtils.copyProperties(certificationDTO.getEditeur(), certification.getEditeur());
		getHibernateTemplate().merge(certification);
	}

	@Override
	public void deleteCertification(int id) {
		Certification certification = (Certification) getHibernateTemplate().load(
				Certification.class, id);
		getHibernateTemplate().delete(certification);
	}

	@Override
	public void deleteCollaborateurCertification(
			CollaborateurCertificationDTO collaborateurCertificationDTO) {
		CollaborateurCertification collaborateurCertification = new CollaborateurCertification();
		collaborateurCertification.setCertification(new Certification());
		collaborateurCertification.getCertification().setEditeur(new Editeur());
		String[] excludePropertiesCertif = {"certification"};
		String[] excludePropertiesEdit = {"editeur"};

		BeanUtils.copyProperties(collaborateurCertificationDTO, collaborateurCertification, excludePropertiesCertif);
		BeanUtils.copyProperties(collaborateurCertificationDTO.getCertification(), collaborateurCertification.getCertification(),excludePropertiesEdit);
		BeanUtils.copyProperties(collaborateurCertificationDTO.getCertification().getEditeur(), collaborateurCertification.getCertification().getEditeur());
		getHibernateTemplate().delete(collaborateurCertification);
	}

}
